package com.androider.timememory.view;

import com.androider.timememory.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class CardImageView extends ImageView {

	final private String TAG = "CardImageView";
	private String cardTitle = null;
	private Paint mPaint;
	private int averageColor = 0xFFF77300;
	private int borderWith = 4;
	private Boolean isSelected = false;
	private Bitmap mBM = null;
	private Resources resources;

	public CardImageView(Context context) {
		super(context);
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.setPadding(5, 5, 5, 0);
		this.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
			}
		});
		resources = context.getResources();
	}

	/**
	 * @param cardTitle the cardTitle to set
	 */
	public void setTitle(String cardTitle) {
		this.cardTitle = cardTitle;
	}
	
	public void setTitle(int cardTitle) {
		this.cardTitle = resources.getString(cardTitle);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);


		if(cardTitle != null){
			mPaint.setColor(0x66060D1F);
			canvas.drawRect(this.getPaddingLeft(), this.getHeight() - this.getPaddingBottom() - 50, this.getWidth() - this.getPaddingRight(), this.getHeight() - this.getPaddingBottom(), mPaint);
			mPaint.setTextAlign(Align.LEFT);
			mPaint.setTextSize(26);
			mPaint.setColor(Color.WHITE);
			canvas.drawText(cardTitle, this.getPaddingLeft() + borderWith + 4, this.getHeight() - this.getPaddingBottom() - 12, mPaint);
		}
		
		/* draw border */
		if(this.isSelected){
			mPaint.setColor(averageColor & 0xFFFFFF + 0x66000000);
		}else{
			mPaint.setColor(averageColor);
		}
		canvas.drawRect(this.getPaddingLeft(), this.getPaddingTop(), this.getPaddingLeft() + borderWith, this.getHeight() - this.getPaddingBottom(), mPaint);
		
		/* draw highlight */
		mPaint.setColor(0x66FFFFFF);
		canvas.drawRect(this.getPaddingLeft(), this.getPaddingTop() + 1, this.getWidth() - this.getPaddingRight(), this.getPaddingTop() + 2, mPaint);
		if(!this.isSelected()){
			canvas.drawRect(this.getPaddingLeft()  + borderWith, this.getPaddingTop(), this.getPaddingLeft() + borderWith + 1, this.getHeight() - this.getPaddingBottom(), mPaint);
		}
		/* draw shadow */
		mPaint.setColor(0x66000000);
		canvas.drawRect(this.getPaddingLeft(), this.getPaddingTop(), this.getWidth() - this.getPaddingRight(), this.getPaddingTop() + 1, mPaint);
		canvas.drawRect(this.getPaddingLeft(), this.getHeight() - this.getPaddingBottom() - 2, this.getWidth() - this.getPaddingRight(), this.getHeight() - this.getPaddingBottom(), mPaint);
		canvas.drawRect(this.getPaddingLeft(), this.getPaddingTop(), this.getPaddingLeft() + 1, this.getHeight() - this.getPaddingBottom(), mPaint);
		
//		Log.i(TAG, "onDraw");
	}

//	@Override
//	public void setImageBitmap(Bitmap bm) {
//		super.setImageBitmap(bm);
//		/* Calculate the average color of bitmap */
//		if(bm != null){
//			float sum = 0;
//			int count = 0;
//			for (int i = 0; i < bm.getHeight(); i++) {
//				for (int j = (int) (bm.getWidth() * 0.7); j < (int) (bm.getWidth() * 0.8); j++) {
//					sum += bm.getPixel(j, i);
//					count++;
//				}
//			}
//			averageColor = (int) (sum/count);
//			averageColor = 0xFFF77300;
//		}
//	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		Log.i(TAG, String.valueOf(event.getAction() & MotionEvent.ACTION_MASK));
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
			case MotionEvent.ACTION_DOWN:
				this.isSelected = true;
				this.postInvalidate();
				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				this.isSelected = false;
				this.postInvalidate();
				break;
			case MotionEvent.ACTION_MOVE:
				return false;
		}
		return super.onTouchEvent(event);
	}


}
